package com.dml.webmagicd.webmagic.start.Impl;

import com.alibaba.fastjson.JSONObject;
import com.dml.webmagicd.webmagic.Processor.JuejinProcessor;
import com.dml.webmagicd.webmagic.pipeline.JuejinPipeline;
import com.dml.webmagicd.webmagic.start.Start;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.HttpRequestBody;

import java.util.HashMap;

@Component
public class JuejinStart implements Start {
  @Autowired
  private JuejinPipeline juejinPipeline;

  @Autowired
  private JuejinProcessor juejinProcessor;

  @Override
  public void start() {
    Request request = new Request(
    "https://api.juejin.cn/booklet_api/v1/booklet/get?aid=2608&uuid=7318006475904419338&spider=0"
    );
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("booklet_id", "7226988578700525605");
    String jsonString = jsonObject.toJSONString();
    request.setRequestBody(HttpRequestBody.json(jsonString, "UTF-8"));
    request.setMethod("POST");
    Spider.create(juejinProcessor)
            .addPipeline(juejinPipeline)
            .addRequest(request)
            .thread(5)
            .run();
  }
}
